package com.wutong.resonance.model.util;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author wutong
 */
public class StringUtil {

    public static int[] stringToIntegerArray(String str, int[] arr) {
        String cleanedStr = str.substring(1, str.length() - 1);
        String[] strArr = cleanedStr.split(",");
        for (int i = 0; i < strArr.length; i++) {
            if (!strArr[i].trim().isEmpty()) {
                arr[i] = Integer.parseInt(strArr[i].trim());
            }
        }
        return arr;
    }

    public static double[] stringToDoubleArray(String str, double[] arr) {
        String cleanedStr = str.substring(1, str.length() - 1);
        String[] strArr = cleanedStr.split(",");
        for (int i = 0; i < strArr.length; i++) {
            if (!strArr[i].trim().isEmpty()) {
                arr[i] = Double.parseDouble(strArr[i].trim());
            }
        }
        return arr;
    }

    public static float[] stringToFloatArray(String str, float[] arr) {
        String cleanedStr = str.substring(1, str.length() - 1);
        String[] strArr = cleanedStr.split(",");
        for (int i = 0; i < strArr.length; i++) {
            if (!strArr[i].trim().isEmpty()) {
                arr[i] = Float.parseFloat(strArr[i].trim());
            }
        }
        return arr;
    }

    public static float[][] stringToFloatMatrix(String str, float[][] arr) {
        String cleanedStr = str.substring(1, str.length() - 1);
        String pattern = "\\[(.*?)]";
        Pattern regex = Pattern.compile(pattern);
        Matcher matcher = regex.matcher(cleanedStr);
        int row = 0;
        while (matcher.find()) {
            String subStr = matcher.group(1);
            String[] strArr = subStr.split(",");
            for (int i = 0; i < strArr.length; i++) {
                arr[row][i] = Float.parseFloat(strArr[i].trim());
            }
            row++;
        }
        return arr;
    }


    public static short[] stringToShortArray(String str, short[] arr) {
        String cleanedStr = str.substring(1, str.length() - 1);
        String[] strArr = cleanedStr.split(",");
        for (int i = 0; i < strArr.length; i++) {
            if (!strArr[i].trim().isEmpty()) {
                arr[i] = Short.parseShort(strArr[i].trim());
            }
        }
        return arr;
    }
}
